# -*- coding: utf-8 -*-
import getopt

def parse_opts(args, options, state):
    opts = ''
    longopts = []
    arg_map = {}
    def_map = {}
    for opt in options:
        if len(opt) == 5:
            short, name, default, message, dummy = opt
        else:
            short, name, default, message = opt
        arg_map['-' + short] = arg_map['--' + name] = name
        def_map[name] = default
        state[name] = default
        if not(default is None or default is True or default is False):
            if short:
                short += ':'
            if name:
                name += '='
        opts += short
        longopts.append(name)
    opts, args = getopt.getopt(args, opts, longopts)
    for k, v in opts:
        opt = arg_map[k]
        t = type(def_map[opt])
        if t is type(1):
            state[opt] = int(v)
        elif t is type(''):
            state[opt] = v
        elif t is type(None) or t is type(False):
            state[opt] = True
    return args
